package com.mriassl.stream.consumer.receiver;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.mriassl.stream.consumer.lib.rabbitmq.RabbitMQConst;
import com.mriassl.stream.consumer.service.BillingService;

@Component
public class BettingReceiver {
    private Pattern pattern = Pattern.compile("^busno:(\\w+),uid:(\\d+),price:(\\d+)$");
//    private Pattern pattern = Pattern.compile("^busno:(\\w+),uid:(\\d+),price:(\\d+),from:(\\w+)$");

    @Autowired
    private BillingService billingService;

    @RabbitListener(queues = RabbitMQConst.QUEUE_FOR_BETTING)
    public void processBettingOrder(String betinfo) {
        if (StringUtils.isBlank(betinfo)) return;
        Matcher matcher = pattern.matcher(betinfo);
        if (matcher.find()) {
            String busno = matcher.group(1);
            Long userid = Long.parseLong(matcher.group(2));
            BigDecimal price = new BigDecimal(matcher.group(3));
//            try {
                billingService.saveBilling(busno, userid, price);
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                System.out.println(format.format(new Date())+"：成功保存投注账单");
//            } catch (Exception e) {
//                System.out.println("tttt=========="+e.getMessage());
//            }
        }
    }

}
